#!/usr/bin/env python
#coding=utf-8
'''
Created on Jul 22, 2010

@author: lxd
'''
import wx
from common.tools import Tools
import bin_blog

class MainFrame(wx.Frame):
    def __init__(self):
        wx.Frame.__init__(self, None, -1, title=u'日志源管理', style=wx.DEFAULT_FRAME_STYLE)        
        self.palMain = wx.Panel(self)                       
        self.createTop()  
        self.infoSizer = self.createInfoSizer()                  
        self.createButton()      
        self.btnSizer = self.createBtnSizer()       
        self.LstrlContent = self.createLstrlContent()
        self.layout()        
        self.bind()
                
        self.loadRssData()
        self.Center() 

    def bind(self):
        self.Bind(wx.EVT_BUTTON, self.OnTestClick, self.btnTest)
        self.Bind(wx.EVT_BUTTON, self.OnAddClick, self.btnAdd)
        self.Bind(wx.EVT_BUTTON, self.OnDelClick, self.btnDel)
        self.Bind(wx.EVT_BUTTON, self.OnSaveClick, self.btnSave)
        self.Bind(wx.EVT_BUTTON, self.OnLookClick, self.btnLook)
        self.Bind(wx.EVT_LIST_ITEM_SELECTED, self.OnItemSelect, self.LstrlContent)
        self.Bind(wx.EVT_CLOSE, self.OnClose)

    def createButton(self):
        self.btnAdd = wx.Button(self.palMain, -1, u'添加')
        self.btnDel = wx.Button(self.palMain, -1, u'删除')
        self.btnDel.Enable(False)
        self.btnSave = wx.Button(self.palMain, -1, u'保存到数据库')
        self.btnLook = wx.Button(self.palMain, -1, u'查看')

    def createTop(self):
        self.lblMessage = wx.StaticText(self.palMain, -1, u"名称:")
        self.txtMessage = wx.TextCtrl(self.palMain, -1, "", size=(175, -1))
        self.lblUrl = wx.StaticText(self.palMain, -1, u"url:")
        self.txtUrl = wx.TextCtrl(self.palMain, -1, "", size=(450, -1))
        self.btnTest = wx.Button(self.palMain, -1, u'测试')
        
    def createInfoSizer(self):
        infoSizer = wx.BoxSizer(wx.HORIZONTAL)
        infoSizer.Add(self.lblMessage, 0, wx.ALL | wx.ALIGN_CENTER_VERTICAL, 5)
        infoSizer.Add(self.txtMessage, 0, wx.ALL | wx.ALIGN_CENTER_VERTICAL, 5)
        infoSizer.Add(self.lblUrl, 0, wx.ALL | wx.ALIGN_CENTER_VERTICAL, 5)
        infoSizer.Add(self.txtUrl, 0, wx.ALL | wx.ALIGN_CENTER_VERTICAL, 5)
        infoSizer.Add(self.btnTest, 0, wx.ALL, 5)
        return infoSizer
    
    def createBtnSizer(self):
        btnSizer = wx.BoxSizer(wx.HORIZONTAL)
        btnSizer.Add(self.btnAdd, 0, wx.ALL, 5)
        btnSizer.Add(self.btnDel, 0, wx.ALL, 5)
        btnSizer.Add(self.btnSave, 0, wx.ALL, 5)
        btnSizer.Add(self.btnLook, 0, wx.ALL, 5)
        return btnSizer
                
    def createLstrlContent(self):
        lstrlContent = wx.ListCtrl(self.palMain, -1, size=(360, 360), style=wx.LC_REPORT | wx.LC_VRULES | wx.LC_HRULES)
        
        lstrlContent.InsertColumn(0, u"名称")
        lstrlContent.InsertColumn(1, u"url")
        lstrlContent.SetColumnWidth(0, 175)
        lstrlContent.SetColumnWidth(1, 540)

        return lstrlContent
   
    def layout(self):
        mainSizer = wx.BoxSizer(wx.VERTICAL)
        mainSizer.Add(self.infoSizer, 0, wx.ALL | wx.EXPAND, 5)
        mainSizer.Add(self.btnSizer, 0, wx.ALL | wx.EXPAND, 5)
        mainSizer.Add(self.LstrlContent, 1, wx.ALL | wx.EXPAND, 5)
        self.palMain.SetSizer(mainSizer)
        mainSizer.Fit(self)
        mainSizer.SetSizeHints(self)  
        
    #############################################    
    def OnAddClick(self, event):
        name = self.txtMessage.GetValue()
        url = self.txtUrl.GetValue()
        index = self.LstrlContent.InsertStringItem(0, name)
        self.LstrlContent.SetStringItem(index, 1, url)
        self.txtMessage.Clear()
        self.txtUrl.Clear() 
    
    def OnDelClick(self, event):
        index = self.LstrlContent.GetFirstSelected()
        self.LstrlContent.deleteItem(index)
        self.txtMessage.Clear()
        self.txtUrl.Clear()        
        self.btnDel.Enable(False)
        
    def OnTestClick(self, event):
        name = self.txtMessage.GetValue()
        url = self.txtUrl.GetValue()
        rss = [dict(name=name, url=url)]
        blog = bin_blog.MainFrame(rss)
        blog.Show()  
            
    def OnLookClick(self, event):
        blog = bin_blog.MainFrame(self.countRss())
        blog.Show()  
   
    def OnSaveClick(self, event):
        rss = self.countRss()
        Tools.saveData(r'db/rss', rss)
    
    def OnItemSelect(self, event):
        index = event.GetIndex()
        self.txtMessage.SetValue(self.LstrlContent.GetItem(index, 0).Text)
        self.txtUrl.SetValue(self.LstrlContent.GetItem(index, 1).Text)
        self.btnDel.Enable(True)
                
    def OnClose(self, event):
        new_rss = self.countRss()
        rss = Tools.loadData(r'db/rss')  
        if new_rss:      
            if (rss and new_rss != rss) or not rss: 
                dlg = wx.MessageDialog(None, u'RSS源已更改，要保存吗？', '友情提示', wx.YES_NO | wx.CANCEL | wx.ICON_QUESTION)
                retCode = dlg.ShowModal()    
                if retCode == wx.ID_YES:
                    Tools.saveData(r'db/rss', new_rss)
                    self.Destroy()
                elif retCode == wx.ID_NO:
                    self.Destroy()
            else:
                self.Destroy()                      
        else:
            self.Destroy()  
                    
    def countRss(self):
        rss = []
        count = self.LstrlContent.GetItemCount()
        for i in range(count): 
            name = self.LstrlContent.GetItem(i, 0).Text
            url = self.LstrlContent.GetItem(i, 1).Text
            rss.append(dict(name=name, url=url))        
        return rss
    
    def loadRssData(self):
        rss = Tools.loadData(r'db/rss')
        if rss:
            rss.reverse()
            for r in rss:
                index = self.LstrlContent.InsertStringItem(0, r['name'])
                self.LstrlContent.SetStringItem(index, 1, r['url'])  
                 
if __name__ == "__main__":
    app = wx.App()
    MainFrame().Show()
    app.MainLoop()


